package com.bjwl.web.contrlller.branchstore;

import com.bjwl.pojo.*;
import com.bjwl.pojo.dto.CardBrDto;
import com.bjwl.pojo.vo.OpenCardVo;
import com.bjwl.service.*;
import com.bjwl.utils.PageBean;
import org.apache.taglibs.standard.lang.jstl.test.PageContextImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: czc
 * @Date:Create：in 2020/4/28 13:41
 * @Description:
 * @Modified By：
 * @Version: 1.0
 */
@Controller
@RequestMapping("/ocard")
public class CardController {

    @Autowired
    private BusinessService businessService;

    @Autowired
    private CardService cardService;

    @Autowired
    private BranchStoreService branchStoreService;

    @Autowired
    private DailyService dailyService;


    //开卡start*********************************************************************************
    @RequestMapping("/jumpOpenCard")
    public String jumpOpenCard(HttpSession httpSession){

        return "branch_oa/send-card";
    }
    @RequestMapping("/jumpRechargeCard")
    public String jumpRechargeCard(HttpSession httpSession){
        List<CardDenomination> denominationList = cardService.findCardAllCarddenomination();
        httpSession.setAttribute("denominationList",denominationList);
        return "branch_oa/recharge-card";
    }

    @RequestMapping("/jumpOpenCardS")
    public String jumpOpenCardS(HttpSession httpSession, Integer jsbuid){
        httpSession.setAttribute("jsbuid",jsbuid);
        Business businessaa = businessService.updateShowBusinese(jsbuid);
        httpSession.setAttribute("businessaa",businessaa);
        Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");

        //开卡页面回显面额
        List<CardDenomination> denominationListss = cardService.findCardAllCarddenomination();
        httpSession.setAttribute("denominationListss",denominationListss);
        //查询所有未开卡的cardid回显在开卡选择卡号的下拉框中
        List<Card> cardList = cardService.findAllCardId(bsadmin.getBsid());
        httpSession.setAttribute("cardList",cardList);
        return "branch_oa/send-card";
    }

    @RequestMapping("/openCard")
    public @ResponseBody Boolean openCard(OpenCardVo openCardVo){
          openCardVo.setState(1);
          openCardVo.setCrateperiod(new Date());
          cardService.openCard(openCardVo);//根据家政卡号去开卡（修改补充家政卡信息）
          return true;
    }


    @InitBinder
    public void initDateFormate(WebDataBinder dataBinder) {
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
        //开卡end*************************************************************************************

    //家政卡列表start******************************************************************************
    @RequestMapping("/jumpCardList")
    private String jumpCardList(){
        return "branch_oa/card-list";
    }

    @RequestMapping("findAllCardList")
    public String findAllCardList(HttpSession httpSession,Integer buid){
        List<Card> cardList = cardService.findAllCardList(buid);
        httpSession.setAttribute("cardMessageList",cardList);
        return "branch_oa/card-list";
    }

    /*<!--查询单条家政卡信息-->*/
    @RequestMapping("/jumpfindCard")
    public String jumpfindCard(HttpSession httpSession ,Integer id){
        //根据id去查询当前选择家政卡的信息
        Card card = cardService.findCardById(id);
        //根据家政卡信息的cdid去查询对应的面额值
        Float denomination  =  cardService.findDenominations(card.getCdid());
        httpSession.setAttribute("denomination",denomination);
        httpSession.setAttribute("card",card);
        return "branch_oa/card-edit";
    }


    //家政卡列表end******************************************************************************

    //家政卡充值信息核对
    @RequestMapping("/findCardByCardId")
    public @ResponseBody Boolean findCardByCardId(HttpSession httpSession,Integer cardtype,Integer cardid){
        Card cardCZ = cardService.findCardByCardId(cardtype,cardid);

        if(cardCZ!=null ){
            httpSession.setAttribute("cardCZ",cardCZ);

            return true;
        }else {
            return false;
        }
    }



    //家政卡充值
    @RequestMapping("/rechargeCard")
    public @ResponseBody Boolean rechargeCard(HttpSession httpSession ,Card card,Float rechargehours){
        if(card.getCardtype()==0){
            if(rechargehours!=null ){
                float nworkinghours = card.getNworkinghours()+rechargehours;
                float yworkinghours = card.getYworkinghours()+rechargehours;
                card.setNworkinghours(nworkinghours);
                card.setYworkinghours(yworkinghours);
                cardService.rechargeHoursCard(card);

                //添加日志家政卡充值工时
                Daily daily = new Daily();
                daily.setTime(new Date());
                daily.setContent("卡号:"+card.getCardid()+"--充值工时："+rechargehours);
                Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
                daily.setBsid(bsadmin.getBsid());
                dailyService.addDaily(daily);

                httpSession.removeAttribute("cardCZ");
                return true;
            }else{
                httpSession.removeAttribute("cardCZ");
                return false;
            }

        }else {
            if(card.getCdid() != 0){
                //根据页面传的面额外键去查询当前面额外键对应的面额值
                float denominations =  cardService.findDenominations(card.getCdid() );
                float ybalance = card.getYbalance()+denominations;
                float nbalance = card.getNbalance()+denominations;
                card.setYbalance(ybalance);
                card.setNbalance(nbalance);
                cardService.rechargeMoneyCard(card);


                //添加日志家政卡充值工时
                Daily daily = new Daily();
                daily.setTime(new Date());
                daily.setContent("卡号:"+card.getCardid()+"--充值现金："+denominations);
                Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
                daily.setBsid(bsadmin.getBsid());
                dailyService.addDaily(daily);

                httpSession.removeAttribute("cardCZ");
                return true;
            }else {
                httpSession.removeAttribute("cardCZ");
                return false;
            }

        }

    }





    //********************************
   /* @RequestMapping("/findCardBybsid")
    public String findCardBybsid(HttpSession httpSession, Integer bsid,Integer pageNum,Integer cardid ,String cardvalue , String wstarttime ,String wendtime,Integer state) {
        if(pageNum == null){
            pageNum =1;
        }

        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        bsid = bsadmin1.getBsid();
        PageBean<Card> cardsList = cardService.findCardBybsid(pageNum,bsid,cardid, cardvalue,wstarttime,wendtime,state);
        httpSession.setAttribute("cardsList", cardsList);
        return "branch_oa/card-list";

    }*/
    @RequestMapping("/findCardBybsid")
    public String findCardBybsid(HttpSession httpSession,HttpServletRequest request, Integer bsid, Integer pageNum, Integer cardid , String cardvalue , String wstarttime , String wendtime, Integer state) {
        if(pageNum == null){
            pageNum =1;
        }
        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        bsid = bsadmin1.getBsid();
        PageBean<Card> cardsList = cardService.findCardBybsid(pageNum,bsid,cardid, cardvalue,wstarttime,wendtime,state);
        CardBrDto cardBrDto = new CardBrDto(cardid,cardvalue,wstarttime,wendtime,state);
        request.setAttribute("cardBrDto",cardBrDto);
        request.setAttribute("cardsList", cardsList);

        return "branch_oa/card-list";
    }

    //********************************

    //根据oid删除当前派单信息
    @RequestMapping("/deleteCard")
    public @ResponseBody Boolean deleteCard(Integer id){
        cardService.deleteCardStrip(id);

        return true;
    }

    @RequestMapping("/updateCardByid")
    public @ResponseBody Boolean updateCardByid(HttpSession httpSession,Card card){
        Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
        BranchStore branchStore = branchStoreService.findbStoreById(bsadmin.getBsid());
        String password = branchStore.getContacts();
        String pass = card.getFit();
       if(pass.equals(password)){
            cardService.updateCardByid(card);
            return true;
        }else {
            return false;
        }


    }

    //创建家政卡
    @RequestMapping("/createCardBs")
    public @ResponseBody Boolean createCardBs(@RequestBody Integer cardid ,HttpSession httpSession){
        /*去查询所有卡号*/
        Card cardsql = cardService.SfindCardByCardId(cardid);
        if(cardsql==null){
            Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
            Card card = new Card();
            card.setBsid(bsadmin.getBsid());
            card.setState(0);
            card.setCardid(cardid);
            cardService.addCreateCard(card);
            return true;
        }else {
            return false;
        }


    }

}
